from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

class Restaurant(models.Model):
    name = models.CharField(max_length=255, verbose_name='餐厅名称')
    description = models.TextField(verbose_name='描述')
    address = models.CharField(max_length=255, verbose_name='地址')
    image = models.ImageField(upload_to='restaurant_images/', verbose_name='图片')
    rating = models.FloatField(default=0.0, verbose_name='评分')
    is_open = models.BooleanField(default=True, verbose_name='是否营业')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '餐厅'
        verbose_name_plural = '餐厅'
        ordering = ['-created_at']

    def __str__(self):
        return self.name

class MenuItem(models.Model):
    CATEGORY_CHOICES = [
        ('主食', '主食'),
        ('小吃', '小吃'),
        ('饮品', '饮品'),
        ('套餐', '套餐'),
    ]
    
    restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE, related_name='menu_items', verbose_name='餐厅')
    name = models.CharField(max_length=255, verbose_name='菜品名称')
    description = models.TextField(blank=True, verbose_name='描述')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    image = models.ImageField(upload_to='menu_items/', blank=True, null=True, verbose_name='图片')
    category = models.CharField(max_length=50, choices=CATEGORY_CHOICES, default='主食', verbose_name='类别')
    is_available = models.BooleanField(default=True, verbose_name='是否可用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '菜品'
        verbose_name_plural = '菜品'
        ordering = ['category', 'name']

    def __str__(self):
        return f"{self.name} - {self.restaurant.name}"

class OrderItem(models.Model):
    order = models.ForeignKey('Order', on_delete=models.CASCADE, related_name='items', verbose_name='订单')
    menu_item = models.ForeignKey(MenuItem, on_delete=models.CASCADE, verbose_name='菜品')
    quantity = models.IntegerField(verbose_name='数量')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    
    class Meta:
        verbose_name = '订单项'
        verbose_name_plural = '订单项'

    def __str__(self):
        return f"{self.menu_item.name} x {self.quantity}"
    
    @property
    def subtotal(self):
        return self.price * self.quantity

class CartItem(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    menu_item = models.ForeignKey(MenuItem, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = ('user', 'menu_item')

    @property
    def subtotal(self):
        return self.menu_item.price * self.quantity

class Order(models.Model):
    STATUS_CHOICES = [
        ('待支付', '待支付'),
        ('已支付', '已支付'),
        ('已取消', '已取消'),
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE, verbose_name='餐厅')
    total_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='总金额')
    status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='待支付', verbose_name='状态')
    trade_no = models.CharField(max_length=100, blank=True, null=True, verbose_name='交易号')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    note = models.TextField(blank=True, verbose_name='备注')

    class Meta:
        verbose_name = '订单'
        verbose_name_plural = '订单'
        ordering = ['-created_at']

    def __str__(self):
        return f"订单 #{self.id} - {self.user.username}"

    @property
    def total_items(self):
        return sum(item.quantity for item in self.items.all())